package com.sam.im.heiqun.uis.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.PhpStatusEntivity;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.activities.AboutUsActivity;
import com.sam.im.heiqun.uis.activities.AutoCheckActvity;
import com.sam.im.heiqun.uis.activities.CollectionActivity;
import com.sam.im.heiqun.uis.activities.MyWalletActivity;
import com.sam.im.heiqun.uis.activities.MycircleActivty;
import com.sam.im.heiqun.uis.activities.NewPasswordActivity;
import com.sam.im.heiqun.uis.activities.PersonalInformationActivity;
import com.sam.im.heiqun.uis.activities.PrivacyActivity;
import com.sam.im.heiqun.uis.activities.QRcode;
import com.sam.im.heiqun.utils.ToastUtil;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.fragments.BaseFragment;
import com.yuyh.library.utils.GlideUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class SetFragment extends BaseFragment {

    @BindView(R.id.nick)
    TextView nick;
    @BindView(R.id.user_id)
    TextView userIdText;

    @BindView(R.id.head_image)
    ImageView headImage;
    @BindView(R.id.two_dimension_code)
    ImageView twoDimensionCode;

    @BindView(R.id.privacy)
    LinearLayout privacy;
    @BindView(R.id.version)
    LinearLayout version;
    @BindView(R.id.about_our)
    LinearLayout aboutOur;
    @BindView(R.id.real_name_approve)
    LinearLayout realNameApprove;
    @BindView(R.id.my_wallet)
    LinearLayout myWallet;
    @BindView(R.id.linear_nick)
    LinearLayout linearNick;
    @BindView(R.id.tv_isauth)
    TextView tvisauth ;
    @BindView(R.id.colaction_layout)
    LinearLayout colaction;
    String headImgStr;
    private PGService mPGservice;

    @Override
    public int getContentViewId() {
        return R.layout.fragment_set;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        mPGservice = PGService.getInstance();
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        UserEntivity user = ToolsUtils.getUser();
        if (null==user)return;
        headImgStr = user.getHeadUrl();
        if (headImage != null) {
            GlideUtils.loadHeadCircularImage(getActivity(), headImgStr, headImage);
        }
        String name = ToolsUtils.getNick(user);
        userIdText.setText("ID：" + user.getId());
        if (user.getIsAuth()!=0){
            //没有认证
            tvisauth.setVisibility(View.VISIBLE);
        }
        nick.setText(name);
    }

    /**
     * 点击事件处理
     */
    @OnClick({R.id.head_image, R.id.two_dimension_code, R.id.privacy, R.id.version,
            R.id.colaction_layout,R.id.about_our, R.id.nick, R.id.my_wallet, R.id.real_name_approve, R.id.linear_nick,R.id.circle_layout})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.head_image:   // 个人信息
            case R.id.linear_nick:
            case R.id.nick:
                startActivity(PersonalInformationActivity.class);
                break;

            case R.id.two_dimension_code:// 二维码
                Intent intentQR = new Intent(getContext(), QRcode.class);
                if (headImgStr != null) {
                    intentQR.putExtra("img", headImgStr);
                }
                startActivity(intentQR);
                break;

            case R.id.privacy:    // 系统设置
                startActivity(PrivacyActivity.class);
                break;

            case R.id.version: // 版本更新
                //MainActivity.mainActivity.getVersion(true);
                showToast(getResources().getString(R.string.this_is_mustxin));
//                ToastUtil.showtoast(getContext(),"当前已经是最新版本");
                break;

            case R.id.about_our:    // 关于我们
                startActivity(AboutUsActivity.class);
                break;

            case R.id.my_wallet: // 我的钱包
                startActivity(MyWalletActivity.class);
                break;

            case R.id.real_name_approve:    // 实名认证
                //startActivity(AutoCheckActvity.class);
                if (ToolsUtils.getUser().getIsAuth()==0){
                    startActivity(AutoCheckActvity.class);
                }
               // authcheck();//认证状态错误
                break;
            case R.id.circle_layout://相册
                startActivity(MycircleActivty.class);
                break;
            case R.id.colaction_layout://收藏
                startActivity(CollectionActivity.class);
                break;
        }
    }

    /**
     * 认证状态
     */
    private void authcheck() {
       // getContext().showProgress("");
        mPGservice.authcheck(ToolsUtils.getMyUserId()).subscribe(new AbsAPICallback<PhpStatusEntivity>() {
            @Override
            public void onNext(PhpStatusEntivity friendEntivity) {
                Log.i("info","state=="+friendEntivity.getInfo().getStatus());
               // MainActivity.mainActivity.hideProgress();
                if ("0".equals(friendEntivity.getInfo().getStatus())) {
                    Intent intent = new Intent(getContext(), NewPasswordActivity.class);
                    intent.putExtra("type", getResources().getString(R.string.real_name_approve));
                    startActivity(intent);
                } else if ("1".equals(friendEntivity.getInfo().getStatus())) {
                    showToast(getResources().getString(R.string.set_renzheng_y));
                } else if ("2".equals(friendEntivity.getInfo().getStatus())) {
                    showToast(getResources().getString(R.string.set_renzheng_y_n));
                }
            }

            @Override
            protected void onResultError(ApiException ex) {
                Log.i("info",""+ex.getCode()+ex.getDisplayMessage());
              //  MainActivity.mainActivity.hideProgress();
            }
        });
    }

}
